﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using CommandLib;

namespace BackEnd.Controllers
{
    public class SlideShowController : Controller
    {
        //
        // GET: /SlideShow/
        [CustomAuthorize(Action = Constants.AdminActions.CreateSlideShow)]
        public ActionResult Index()
        {
            SlideShowModel slideShowModel = new SlideShowModel();
            SlideShow slideShow = new SlideShowBL().GetTop();
            if (slideShow != null)
            {
                slideShowModel.Id = slideShow.Id;
                slideShowModel.TimeWaiting = slideShow.TimeWaiting;
                slideShowModel.ListSlideShowDetail = new SlideShowDetailBL().GetBySlideShowId(slideShow.Id);
            }

            return View(slideShowModel);
        }

        [HttpPost]
        public JsonResult Create(string timeWaiting, string file)
        {
            string user = (string)Session[Common.Constants.SessionKeys.UserInfo];
            string permissionActions = (string)Session[Common.Constants.SessionKeys.PermissionActions];
            bool hasPermission = false;
            if (String.IsNullOrEmpty(user))
            {
                return Json(-2, JsonRequestBehavior.AllowGet);
            }
                                            
            hasPermission = permissionActions != null && permissionActions.Contains(Constants.AdminActions.CreateSlideShow);
            if (hasPermission == false)
            {
                return Json(-3, JsonRequestBehavior.AllowGet);
            }
            if (!string.IsNullOrEmpty(timeWaiting))
            {
                int slideShowId;
                new SlideShowBL().Insert(new SlideShow { TimeWaiting = Convert.ToInt32(timeWaiting) }, out slideShowId);
                new SlideShowDetailBL().DeleteBySlideShowId(slideShowId);
                if (!string.IsNullOrEmpty(file))
                {
                    string[] arrayFile = file.Split(';');                                        
                    for (int i = 0; i < arrayFile.Length - 1; i++)
                    {
                        string fileName = string.Empty;
                        string redirectUrl = string.Empty;
                        try
                        {
                            fileName = arrayFile[i].Split('#')[0];
                            redirectUrl = arrayFile[i].Split('#')[1];
                        }
                        catch { }
                        new SlideShowDetailBL().Insert(new SlideShowDetail { SlideShowId = slideShowId, Image = fileName, ImageUrl = Config.FolderImageSlideShow, RedirectUrl=redirectUrl });
                    }
                }                
                return Json(1, JsonRequestBehavior.AllowGet);
            }
            else
            {                
                return Json(-1, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult UploadFile()
        {
            string imageName = string.Empty;
            string fileName = string.Empty;
            int errorCode = 100;            
            try
            {
                var files = Request.Files;
                if (files.Count > 0)
                {
                    if (!Directory.Exists(Config.FolderImageSlideShow))
                    {
                        Directory.CreateDirectory(Config.FolderImageSlideShow);
                    }
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFileBase file = files[i];
                        imageName = file.FileName;
                        var lastIndexOfDot = imageName.LastIndexOf(".");
                        if (lastIndexOfDot > -1)
                        {
                            fileName = string.Format("{0}_{1}{2}", file.FileName.Substring(0, lastIndexOfDot - 1),
                                DateTime.Now.Ticks.ToString(),
                                file.FileName.Substring(lastIndexOfDot));
                            string filePath = Path.Combine(
                                Config.FolderImageSlideShow,
                                fileName);
                            file.SaveAs(filePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errorCode = -999;
            }
            return Json(new { errorCode, fileName },JsonRequestBehavior.AllowGet);
        }

    }
}
